{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1da262d0",
   "metadata": {},
   "source": [
    "已知，某服务固定和浮动成本占比1:1，此时请求数为1000,总成本为1000，当前请求单价price为1.0，当请求数波动时浮动成本线性波动。假设，当前有2个用户A和B，请求数分别为500和500。未来一个月A和B的流量，以(A,B)表示，存在以下波动\n",
    "\n",
    "1. (500,100)\n",
    "2. (500,1000)\n",
    "3. (400,200)\n",
    "4. (600,600)\n",
    "\n",
    "问，如何设计给A和B的账单，通过price调节驱使用它们增加请求数？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "09a2815f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(500, 100),总成本:800, 价格为1.333\n",
      "(500, 1000),总成本:1250, 价格为0.833\n",
      "(400, 200),总成本:800, 价格为1.333\n",
      "(600, 600),总成本:1100, 价格为0.917\n",
      "===if price is not change\n",
      "(500, 100)\n",
      "新成本:800 (666, 133) \t 旧成本：600 (500, 100) \t差异-200 (-166, -33) \n",
      "(500, 1000)\n",
      "新成本:1250 (416, 833) \t 旧成本：1500 (500, 1000) \t差异250 (83, 166) \n",
      "(400, 200)\n",
      "新成本:800 (533, 266) \t 旧成本：600 (400, 200) \t差异-200 (-133, -66) \n",
      "(600, 600)\n",
      "新成本:1100 (550, 550) \t 旧成本：1200 (600, 600) \t差异100 (50, 50) \n",
      "===if price change\n"
     ]
    }
   ],
   "source": [
    "price=1.0\n",
    "a1=(500,100)\n",
    "a2=(500,1000)\n",
    "a3=(400,200)\n",
    "a4=(600,600)\n",
    "\n",
    "fixed_cost=1000*0.5\n",
    "nofixed_cost=1000*0.5\n",
    "traffic=1000\n",
    "# 服务price波动如下：\n",
    "def foo(atuple2):\n",
    "    total_cost=fixed_cost+nofixed_cost*1.0*(atuple2[0]+atuple2[1])/traffic\n",
    "    total_price=total_cost/(atuple2[0]+atuple2[1])\n",
    "    print('{},总成本:{}, 价格为{:.3f}'.format(atuple2,int(total_cost),total_price))\n",
    "foo(a1)\n",
    "foo(a2)\n",
    "foo(a3)\n",
    "foo(a4)\n",
    "print('===if price is not change')\n",
    "\n",
    "\n",
    "def foo(atuple2):\n",
    "    total_cost=fixed_cost+nofixed_cost*1.0*(atuple2[0]+atuple2[1])/traffic\n",
    "    total_price=total_cost/(atuple2[0]+atuple2[1])\n",
    "    cost_0=atuple2[0]*price\n",
    "    cost_1=atuple2[1]*price    \n",
    "    new_cost_0=atuple2[0]*total_price\n",
    "    new_cost_1=atuple2[1]*total_price    \n",
    "    print('{}\\n新成本:{} {} \\t 旧成本：{} {} \\t差异{} {} '.format(atuple2\n",
    "                                               ,int(total_cost)\n",
    "                                            ,(int(new_cost_0),int(new_cost_1))\n",
    "                                               ,int(cost_0+cost_1)\n",
    "                                               ,(int(cost_0),int(cost_1))\n",
    "                                               ,int(cost_0+cost_1-total_cost)\n",
    "                                               ,(int(cost_0-new_cost_0),int(cost_1-new_cost_1))\n",
    "         ))\n",
    "\n",
    "          \n",
    "foo(a1)\n",
    "foo(a2)\n",
    "foo(a3)\n",
    "foo(a4)\n",
    "\n",
    "print('===if price change')\n",
    "\n",
    "# def foo(atuple2):\n",
    "#     total_cost=fixed_cost+nofixed_cost*1.0*(atuple2[0]+atuple2[1])/traffic\n",
    "#     total_price=total_cost/(atuple2[0]+atuple2[1])\n",
    "#     cost_0=atuple2[0]*price\n",
    "#     cost_1=atuple2[1]*price    \n",
    "    \n",
    "#     new_atuple2[]=(atuple2[0]/500.0)*total_price\n",
    "#     new_cost_1=atuple2[1]*total_price    \n",
    "#     print('{}\\n新成本:{} {} \\t 旧成本：{} {} \\t差异{} {} '.format(atuple2\n",
    "#                                                ,int(total_cost)\n",
    "#                                             ,(int(new_cost_0),int(new_cost_1))\n",
    "#                                                ,int(cost_0+cost_1)\n",
    "#                                                ,(int(cost_0),int(cost_1))\n",
    "#                                                ,int(cost_0+cost_1-total_cost)\n",
    "#                                                ,(int(cost_0-new_cost_0),int(cost_1-new_cost_1))\n",
    "#          ))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ff971a40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function tuple.count(value, /)>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1.count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "28b1259a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21436.510000000002"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6425.79+7251.96+7758.76"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddc14c91",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
